package com.crois.barrier.web.support;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hou Ze Yu
 * @description
 * @date 2020/10/16
 */
public class ParamMappingUtils {
    /**
     * query map转 String
     * @param query
     * @return
     */
    public static String queryMapToString(MultiValueMap<String, String> query){
        String queryString = "";
        if (CollectionUtils.isNotEmpty(query.keySet())){
            List<String> result = new ArrayList<>(query.keySet());

            for (int i = 0; i < result.size(); i++) {
                if (i==0){
                    if (CollectionUtils.isNotEmpty(query.get(result.get(i)))){
                        queryString= queryString+result.get(i)+"="+query.get(result.get(i)).get(0);
                    }

                }else {
                    if (CollectionUtils.isNotEmpty(query.get(result.get(i)))){
                        queryString= queryString+"&"+result.get(i)+"="+query.get(result.get(i)).get(0);
                    }

                }
            }
        }
        return queryString;
    }
}
